package com.example.text32;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class MainActivity extends AppCompatActivity {

    Button button;
    EditText name, password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = findViewById(R.id.name);
        password = findViewById(R.id.password);
        button = findViewById(R.id.button);
        try {
            FileInputStream input = openFileInput("data.txt");
            InputStreamReader reader = new InputStreamReader(input);
            BufferedReader br = new BufferedReader(reader);
            String s = br.readLine();
            String[] s1 = s.split(" ");
            name.setText(s1[0]);
            password.setText(s1[1]);
            br.close();
            reader.close();
            input.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String names = name.getText().toString();
                String passwords = password.getText().toString();
                if("admin".equals(names)&&"123456".equals(passwords)){
                    Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
                    try {
                        FileOutputStream out = openFileOutput("data.txt", MODE_PRIVATE);
                        String data = names+" "+passwords;
                        out.write(data.getBytes(StandardCharsets.UTF_8));
                        out.close();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }

                }else {
                    Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}